package com.wms.service.information;

import com.wms.common.PageInfo;
import com.wms.common.Utils;
import com.wms.dao.CompanyMapper;
import com.wms.dao.InformationMapper;
import com.wms.domain.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class informationImpl implements InformationService {

    private static final Logger log = LoggerFactory.getLogger(InformationService.class);
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private InformationMapper informationMapper;
    @Transactional
    @Override
    public Object add(CompanyDomain companyDomain) {
        log.info("add comp"+companyDomain.toString());
        int value;
        companyDomain.setCreate_date(Utils.getDate());
        if(companyDomain.getId() != null){
            value = companyMapper.update(companyDomain);
        }else {
            value = companyMapper.add(companyDomain);
        }
        return null;
    }

    @Transactional
    @Override
    public Object update(CompanyDomain companyDomain) {
        companyMapper.update(companyDomain);
        return null;
    }

    @Transactional
    @Override
    public Object delete(Long id) {
        log.info("delete comp"+id);
        companyMapper.delete(id);
        return null;
    }

    @Override
    public Object getCompanyPage(CompanyDomain companyDomain) {
        log.info(companyDomain.toString());
        PageInfo page = new PageInfo();
        page.setData(companyMapper.getCompanyPage(companyDomain));
        page.setCount(companyDomain.getBaseCount());
        page.setSum(companyDomain.getBaseSum());
        return page;
    }

    @Transactional
    @Override
    public Object addOrUpdateClients(ClientsDomain clientsDomain) {
        log.info("add clients"+clientsDomain.toString());
        clientsDomain.setDate(Utils.getDate());
        if(clientsDomain.getId() != null){
            informationMapper.updateClients(clientsDomain);
        }else {
            informationMapper.addClients(clientsDomain);
        }
        return null;
    }

    @Transactional
    @Override
    public Object deleteClients(Long id) {
        informationMapper.deleteClients(id);
        return null;
    }


    @Override
    public Object getClientsPage(ClientsDomain clientsDomain) {
        log.info(clientsDomain.toString());
        PageInfo page = new PageInfo();
        page.setData(informationMapper.getClientsPage(clientsDomain));
        page.setCount(clientsDomain.getBaseCount());
        page.setSum(clientsDomain.getBaseSum());
        return page;
    }

    //storage
    @Transactional
    @Override
    public Object addOrUpdateStorage(StorageDomain storageDomain) {
        storageDomain.setDate(Utils.getDate());
        if(storageDomain.getId() != null){
//            storageDomain.setStore_stat(0L);
            informationMapper.updateStorage(storageDomain);
        }else {
            informationMapper.addStorage(storageDomain);
        }
        return null;
    }

    @Transactional
    @Override
    public Object deleteStorage(Long id) {
        informationMapper.deleteStorage(id);
        return null;
    }

    @Override
    public Object getStoragePage(StorageDomain storageDomain) {
        log.info(storageDomain.toString());
        PageInfo page = new PageInfo();
        page.setData(informationMapper.getStoragePage(storageDomain));
        page.setCount(storageDomain.getBaseCount());
        page.setSum(storageDomain.getBaseSum());
        return page;
    }

    //storeArea
    @Transactional
    @Override
    public Object addOrUpdateStoreArea(StoreAreaDomain storeAreaDomain) {
        if(storeAreaDomain.getId() != null){
            informationMapper.updateStoreArea(storeAreaDomain);
        }else {
            informationMapper.addStoreArea(storeAreaDomain);
        }
        return null;
    }
    @Transactional
    @Override
    public Object deleteStoreArea(Long id) {
        informationMapper.deleteStoreArea(id);
        return null;
    }

    @Override
    public Object getStoreAreaPage(StoreAreaDomain storeAreaDomain) {
        log.info(storeAreaDomain.toString());
        PageInfo page = new PageInfo();
        page.setData(informationMapper.getStoreAreaPage(storeAreaDomain));
        page.setCount(storeAreaDomain.getBaseCount());
        page.setSum(storeAreaDomain.getBaseSum());
        return page;
    }


    //StoreLocation
    @Transactional
    @Override
    public Object addOrUpdateStoreLocation(StoreLocationDomain storeLocationDomain) {
        storeLocationDomain.setDate(Utils.getDate());
        if(storeLocationDomain.getId() != null){
            informationMapper.updateStoreLocation(storeLocationDomain);
        }else {
            informationMapper.addStoreLocation(storeLocationDomain);
        }
        return null;
    }

    @Transactional
    @Override
    public Object deleteStoreLocation(Long id) {
        informationMapper.deleteStoreLocation(id);
        return null;
    }

    @Override
    public Object getStoreLocationPage(StoreLocationDomain storeLocationDomain) {
        log.info(storeLocationDomain.toString());
        PageInfo page = new PageInfo();
        page.setData(informationMapper.getStoreLocationPage(storeLocationDomain));
        page.setCount(storeLocationDomain.getBaseCount());
        page.setSum(storeLocationDomain.getBaseSum());
        return page;
    }
}
